package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.meily.constant.enums.NotifySceneEnum;
import cn.openea.perpartner.meily.constant.enums.NotifySendEnum;
import cn.openea.perpartner.meily.thirdparty.notify.NotifySendEvent;
import cn.openea.perpartner.meily.web.entity.vo.GoodsDeliveryNoticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ysk
 * @version 1.0.1
 * @Description
 * @createTime 2023年04月21日 17:59:00
 */
@Api(tags = "通知发送")
@RestController
@RequestMapping("/notify/send")
public class NotifySendController {

    @Resource
    private ApplicationContext applicationContext;

    @ApiOperation("商品发货通知")
    @PostMapping("/goods/delivery")
    public AjaxResult goodsDeliveryNotify(@RequestBody @Valid GoodsDeliveryNoticeVo noticeVo) {
        Map<String, Object> params = new HashMap<>();
        params.put(NotifySendEnum.SMS.name(), noticeVo);
        try {
            applicationContext.publishEvent(new NotifySendEvent(this,
                    NotifySendEnum.SMS,
                    NotifySceneEnum.GOODS_DELIVERY,
                    params));
        } catch (Exception e) {
            return AjaxResult.error("发送失败，请重试");
        }
        return AjaxResult.success();
    }

}
